package com.jumi.microservice.material.controller;

import com.alibaba.fastjson.JSON;
import com.jumi.common.log.annotation.Log;
import com.jumi.common.log.enums.BusinessType;
import com.jumi.microservice.common.base.AjaxResult;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.constant.HttpStatus;
import com.jumi.microservice.common.constant.RequestConstant;
import com.jumi.microservice.common.exception.BaseException;
import com.jumi.microservice.common.exception.ExceptionSupply;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.material.domain.JmCategory;
import com.jumi.microservice.material.dto.category.JmCategoryDelAllRequest;
import com.jumi.microservice.material.dto.category.JmCategoryFindAllRequest;
import com.jumi.microservice.material.dto.category.JmCategoryRequest;
import com.jumi.microservice.material.service.IJmCategoryService;
import com.jumi.microservice.material.service.JmCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/category")
@Api(tags = {"【基础资料】拆合单分类接口"})
@Service
public class JmCategoryController extends BaseController implements IJmCategoryService {

    private static final Logger log = LoggerFactory.getLogger(JmCategoryController.class);

    @Resource
    private JmCategoryService jmCategoryService;

    /*添数据ok*/
    @PostMapping("/saveJmCategory")
    @ApiOperation("添加绑定类")
    @Log(title = "供应链管理-基础管理-拆合单分类",businessType = BusinessType.INSERT)
    public ResponseResult saveJmCategory(@Validated JmCategoryRequest jmCategoryRequest, @RequestHeader(RequestConstant.ADMIN_NAME) String createBy) throws BaseException {
        JmCategory jmCategory = new JmCategory();
        BeanUtils.copyProperties(jmCategoryRequest, jmCategory);
        jmCategory.setCreateBy(createBy);
        int get = jmCategoryService.saveJmCategory(jmCategory);
        if (get == -1) {
            return ResponseResult.error(HttpStatus.CONFLICT, "已经存在相同的名称!");
        } else if ((get == 0)) {
            return ResponseResult.error(HttpStatus.CONFLICT, "更新失败!");
        } else {
            return ResponseResult.success(jmCategory.getCategoryId());
        }
    }

    /*更新数据*/
    @PostMapping("/updateJmCategory")
    @ApiOperation("更新绑定类")
    @Log(title = "供应链管理-基础管理-拆合单分类",businessType = BusinessType.UPDATE)
    public ResponseResult updateJmCategory(JmCategoryRequest JmCategoryRequest, @RequestHeader(RequestConstant.ADMIN_NAME) String updateBy) {
        JmCategory jmCategory = new JmCategory();
        BeanUtils.copyProperties(JmCategoryRequest, jmCategory);
        jmCategory.setUpdateBy(updateBy);
        int update = jmCategoryService.updateJmCategory(jmCategory);
        if (update == -1) {
            return ResponseResult.error(HttpStatus.CONFLICT, "已经存在相同的名称!");
        } else if ((update == 0)) {
            return ResponseResult.error(HttpStatus.CONFLICT, "更新失败!");
        } else {
            return ResponseResult.success(jmCategory.getCategoryId());
        }
    }

    /*根据id删除指定数据（软删除）*/
    @PostMapping("/deleteJmCategory")
    @ApiOperation("删除指定分类")
    @ApiImplicitParam(name = "ids", value = "分类ID集合")
    @Log(title = "供应链管理-基础管理-拆合单分类",businessType = BusinessType.DELETE)
    public ResponseResult deleteJmCategory(String[] ids, @RequestHeader(RequestConstant.ADMIN_NAME) String updateBy) {
        JmCategory jmCategory = new JmCategory();
        jmCategory.setIds(ids);
        jmCategory.setUpdateBy(updateBy);
        int backInfo = jmCategoryService.deleteBatchByIds(jmCategory);
        return backInfo == 0 ? ResponseResult.error(HttpStatus.CONFLICT, "操作失败!") : ResponseResult.success(jmCategory.getCategoryId());
    }


    /*根据条件查询所有数据*/
    @GetMapping("/findByPage")
    @ApiOperation("查询所有信息")
    public TableDataInfo findByPage(JmCategoryFindAllRequest jmCategoryFindAllRequest) {
        JmCategory jmCategory = new JmCategory();
        BeanUtils.copyProperties(jmCategoryFindAllRequest, jmCategory);
        startPage();
        List<JmCategory> listAll = jmCategoryService.findAll(jmCategory);
        return getDataTable(listAll);
    }

    /*根据条件查询所有数据*/
    @GetMapping("/findOne")
    @ApiOperation("根据id查询指定数据")
    public ResponseResult findById(Long id) {
        JmCategory listOne = jmCategoryService.findById(id);
        return ResponseResult.success(listOne);
    }

    /*列表中更新上下架状态*/
    @PostMapping("/statusById")
    @ApiOperation("更新状态")
    @Log(title = "供应链管理-基础管理-拆合单分类",businessType = BusinessType.UPDATE)
    public ResponseResult statusById(JmCategoryRequest jmCategoryRequest, @RequestHeader(RequestConstant.ADMIN_NAME) String updateBy) {
        JmCategory jmCategory = new JmCategory();
        BeanUtils.copyProperties(jmCategoryRequest, jmCategory);
        jmCategory.setUpdateBy(updateBy);
        if (jmCategory.getStatus() == null) {
            throw new ExceptionSupply(500, "状态值不能为空").newException();
        }
        int getNum = jmCategoryService.statusById(jmCategory);
        return getNum == 0 ? ResponseResult.error(HttpStatus.CONFLICT, "操作失败!") : ResponseResult.success(jmCategory.getCategoryId());
    }

    /*批量删除拆合单分类*/
    @GetMapping("/deleteAllByIds")
    @ApiOperation("根据id批量删除拆合单分类")
    @Log(title = "供应链管理-基础管理-拆合单分类",businessType = BusinessType.DELETE)
    public ResponseResult deleteAllByIds(@Validated JmCategoryDelAllRequest jmCategoryDelAllRequest, @RequestHeader(RequestConstant.ADMIN_NAME) String updateBy) {
        JmCategory jmCategory = new JmCategory();
        String ids = jmCategoryDelAllRequest.getIds();
        String[] idsInfo = ids.split(",");
        jmCategory.setIds(idsInfo);
        jmCategory.setUpdateBy(updateBy);
        int delListNum = jmCategoryService.deleteBatchByIds(jmCategory);
        return delListNum == 0 ? ResponseResult.error(HttpStatus.CONFLICT, "操作失败!") : ResponseResult.success();
    }

    @Override
    @ApiOperation("查询所有分类(api服务调用)")
    public ResponseResult findAll() {
        JmCategory jmCategory = new JmCategory();
        List<JmCategory> listAll = jmCategoryService.findAll(jmCategory);
        log.info("查询所有分类(api服务调用)=》 {}",JSON.toJSONString(listAll));
        return ResponseResult.success(listAll);
    }
}
